package com.kakaxi.mybilibili.module.search.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.HorizontalVideoRecyAdapter;
import com.kakaxi.mybilibili.adapter.entity.HorizontalVideoMultiItem;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.HorizontalVideoItemBean;
import com.kakaxi.mybilibili.module.video.view.VideoActivity;
import com.kakaxi.mybilibili.utils.CastUncheckedUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 综合视频搜索碎片
 * Created by kakaxi on 16/11/24.
 */

public class SearchVideoFragment extends BaseFragment {

    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout refresh;

    @BindView(R.id.recv_common_fragment)
    RecyclerView recyclerView;

    private HorizontalVideoRecyAdapter mAdapter;


    public static SearchVideoFragment newInstance() {
        SearchVideoFragment searchVideoFragment = new SearchVideoFragment();
        searchVideoFragment.setTitle("综合");
        return searchVideoFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);

        // 初始化刷新
        refresh.setEnabled(false);
        // 初始化点击事件
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (i == 0) {
                    return;
                }
                HorizontalVideoItemBean itemBean = (HorizontalVideoItemBean) mAdapter.getItem(i).getData();
                Bundle bundle = new Bundle();
                bundle.putString(VideoActivity.EXTRA_VIDEO_AID, itemBean.getId() + "");
                bundle.putString(VideoActivity.EXTRA_VIDEO_COVER_URL, itemBean.getPic());
                goActivity(VideoActivity.class, bundle);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 显示搜索的数据
     *
     * @param object 事件巴士对象
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onShowVideoEventBus(EventBusObject object) {
        if (object.getEvent() == EventBusObject.EVENT_SEARCH_VIDEO_FRAG_SHOW_SEARCH) {
            List<HorizontalVideoMultiItem> videoMultiItemList = CastUncheckedUtil.cast(object.getData());
            if (mAdapter == null) {
                mAdapter = new HorizontalVideoRecyAdapter(videoMultiItemList);
                recyclerView.setAdapter(mAdapter);
            } else {
                mAdapter.setNewData(videoMultiItemList);
            }
        }
    }

}
